package juntos.lbcv.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;

public class SendAndroidNotif {

	public static void main(String[] args) {
		String gcmRegID = "APA91bHIjO9GWqUlSB1q7Vz0uXrt4UpZpAGXvFKv95722wS18qOAxMQ56acI-FZ9L-9m5gpg0JB8jvfKtyeKJkcoLFJeQt16HsBmQ9jr82CvYmHPcriFkXLIkt6DlniLB1JAB2oVM9A8Y4Kru5-TtdD-Ggim8dlziw";
		 
		Map<String, String> data = new HashMap<String, String>();
		data.put("message", "test reussi");
		SendAndroidNotif.run(gcmRegID, data);
	}

	// The SENDER_ID here is the "Browser Key" that was generated when I
	// created the API keys for my Google APIs project.
	private static final String SENDER_ID = "AIzaSyBjLWClTcHNk6daQrshpNAiyHK25Q8xv60";

	public static void run(String ANDROID_DEVICE, Map<String, String> data) {
		// This array will hold all the registration ids used to broadcast a
		// message.
		// for this demo, it will only have the ANDROID_DEVICE id that was
		// captured
		// when we ran the Android client app through Eclipse.
		List<String> androidTargets = new ArrayList<String>();
		androidTargets.add(ANDROID_DEVICE);

		// Instance of com.android.gcm.server.Sender, that does the
		// transmission of a Message to the Google Cloud Messaging service.
		Sender sender = new Sender(SENDER_ID);
		// This Message object will hold the data that is being transmitted
		// to the Android client devices. For this demo, it is a simple text
		// string, but could certainly be a JSON object.
		Message message = new Message.Builder().collapseKey("collapseKey")
				.timeToLive(30).delayWhileIdle(true).setData(data).build();
		try {
			// use this for multicast messages. The second parameter
			// of sender.send() will need to be an array of register ids.
			MulticastResult result = sender.send(message, androidTargets, 1);

			if (result.getResults() != null) {
				int canonicalRegId = result.getCanonicalIds();
				if (canonicalRegId != 0) {

				}
			} else {
				int error = result.getFailure();
				System.out.println("Broadcast failure: " + error);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("fin");

	}

}